package com.example.mydemo103.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mydemo103.Adapter.ListViewAdapter;
import com.example.mydemo103.MainActivity;
import com.example.mydemo103.R;
import com.example.mydemo103.bean.Book;


import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class BookAllActivity extends AppCompatActivity {
    private LinearLayout bookallbylinertlayout;
    private ImageView bookallbyleft;
    private List<Book> booklist=new ArrayList<>();
    private final static String HTML_URL = "http://192.168.1.102:8080/libraryMs_war/book/book/getBooksByTitle.do?title=";

    OkHttpClient okHttpClient = new OkHttpClient();
    private Handler handler = new Handler() {
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                try {
                JSONObject jsonObject1 = new JSONObject(msg.obj.toString());
                JSONArray jsonArray = jsonObject1.getJSONArray("book");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                    //取出name
                    String bookname = jsonObject.getString("title");
                    String bookauthor = jsonObject.getString("author");
                    String booktext = jsonObject.getString("type");
                    Book book=new Book(bookname,bookauthor,booktext);
                    booklist.add(book);
                    ListViewAdapter listViewAdapter=new ListViewAdapter(BookAllActivity.this,R.layout.book_layout,booklist);
                    ListView listView=findViewById(R.id.bookalllist);
                    listView.setAdapter(listViewAdapter);
//                    JSONArray jarray1 = jsonObject.getJSONArray("author");
//                    JSONArray jarray2 = jsonObject.getJSONArray("type");
                    Log.e("Json", bookname);
                    System.out.println(bookauthor);
                    System.out.println(booktext);
                    System.out.println("所有图书列表:成功");
                }
                System.out.println(booklist);
                System.out.println("所有图书列表");
            } catch (Exception e) {
                e.printStackTrace();
                    System.out.println("所有图书列表:失败");
            }
                break;
            }

            }

    };

    public void AllBook(){
        for(int i=0;i<10;i++){
//            Book bookone=new Book("《天行剑》","余维华","正文");
//            booklist.add(bookone);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookall);

//        String bookname;
//        if (id.equals("0")){
//             bookname =HTML_URL;
//        }else {
//             bookname = HTML_URL + id;
//        }

        new Thread(){
            @Override
            public void run() {
                super.run();
                Request request = new Request.Builder().get().url(HTML_URL).build();

                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        System.out.println("onFailure");
                        System.out.println(call);
                        System.out.println(e);
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        System.out.println("onResponse: ");
                        String content = response.body().string();
                        Message msg = new Message();
                        msg.obj = content;
                        msg.what=1;
                        handler.sendMessage(msg);
                    }
                });
            }
        }.start();

//        绑定当前View界面的incule Id
        bookallbylinertlayout=findViewById(R.id.bookallbyincule);
//        绑定incule中的控件Id
        bookallbyleft=bookallbylinertlayout.findViewById(R.id.bookallleft);
//        设置点击事件
        bookallbyleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BookAllActivity.this, MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
            }
        });

//    去除标题栏
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

    }




    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }






}